package com.xj.admin.base.sysparam.web;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.admin.base.index.web.BaseController;
import com.xj.admin.base.sysparam.entity.TbSysParam;
import com.xj.admin.base.sysparam.service.ITbSysParamService;
import com.xj.admin.util.ExportUtils;
import com.xj.admin.util.JsonUtil;
import com.xj.admin.util.dtgrid.model.Pager;
import com.xj.common.base.common.bean.AbstractBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author ruanzh
 * @since 2017-08-21
 */
@Controller
@RequestMapping("/sysparam/")
public class TbSysParamController extends BaseController {

	@Autowired
	private ITbSysParamService sysparamService;
	
	@GetMapping("listUI")
    public String listUI() {
		return "sysparam/list";
    }
	
	@GetMapping("form")
    public String form(Map<String,Object> map) {
		return "sysparam/form";
    }
    
    @PostMapping("list")
	@ResponseBody
    public Object list(String gridPager) {
		Pager pager = JsonUtil.getObjectFromJson(gridPager, Pager.class);
		Map<String, Object> parameters = pager.getParameters();
		Page<TbSysParam> list = sysparamService.selectPage(new Page<TbSysParam>(pager.getNowPage(), pager.getPageSize()), Condition.create().allEq(pager.getParameters()));
		makeGridResult(parameters, pager, list);
		return parameters;
    }

    @PostMapping("export")
	@ResponseBody
    public void export(String gridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Pager pager = JsonUtil.getObjectFromJson(gridPager, Pager.class);
		ExportUtils.export(request, response, pager);
    }
	
	@PostMapping("save")
	@ResponseBody
	public AbstractBean add(TbSysParam tbsysparam){
		if(!sysparamService.insertOrUpdate(tbsysparam)){
			return error();
		}
		return success();
	}
	
	@DeleteMapping("{id}/delete")
	@ResponseBody
    public AbstractBean delete(@PathVariable(required=true) Integer id) {	
		if(!sysparamService.deleteById(id)){
			return error();
		}
		return success();
    }	
	
	@GetMapping("{id}/select")
    public String select(Map<String,Object> map,@PathVariable(required=true) Integer id) {	
		TbSysParam tbsysparam = sysparamService.selectById(id);
		map.put("record", tbsysparam);
		return "sysparam/edit";
    }	
}
